package com.xk.springboot.controller;

import com.xk.springboot.dto.ResponseData;
import com.xk.springboot.dto.ResultData;
import com.xk.springboot.flag.Login;
import com.xk.springboot.flag.Register;
import com.xk.springboot.pojo.Member;
import com.xk.springboot.utils.ValidateUtil;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author:Teacher黄
 * @date:Created at 2020/04/08
 */
@RestController
@RequestMapping("/member")
@SuppressWarnings("all")
public class MemberController {


    /**
     * 注册的方法
     * @param member
     * @return
     */
    @PostMapping("/reg")
    public ResultData register(@Validated(Register.class) @RequestBody Member member, BindingResult result){

        // 先判断是否有错误
        String errors = ValidateUtil.getErrorMsg(result);
        if (errors.length() > 0) {
            return ResponseData.error(1001,errors);
        }
        return ResponseData.success(member);
    }


    /**
     * 登陆的方法
     * @param member
     * @return
     */
    @PostMapping("/login")
    public ResultData login(@Validated(Login.class) @RequestBody Member member, BindingResult result){
        // 先判断是否有错误
        String errors = ValidateUtil.getErrorMsg(result);
        if (errors.length() > 0) {
            return ResponseData.error(1001,errors);
        }

        return ResponseData.success(member);
    }


}
